package wang.conge.javasedemo.core.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class LockInterruptiblyTest {
	public static void main(String[] args) {
		Lock lock = new ReentrantLock();
		
		Model model = new Model();
		
		Runnable run = (()->{
			try{
				lock.lockInterruptibly();
			}catch(InterruptedException e){
				System.out.println("Interrupt");
				return;
			}
			
			try {
				model.addNum();
				
				System.out.println(model.getNum());
			} finally {
				lock.unlock();
			}
			
		}) ;
		
		int threadNum = 100;
		Thread [] threads = new Thread[threadNum];
		for(int i=0;i<threadNum;i++){
			threads[i] = new Thread(run);
			threads[i].start();
		}
		
		for(int i=0;i<threadNum;i++){
			if(i%10==0){
				threads[i].interrupt();
			}
		}
	}
	
	static class Model {
		private int num = 0;

		public int getNum() {
			return num;
		}

		public void addNum() {
			num = num + 1;
		}
	}
	
	
}
